package com.zhanghe.study.springboot.configuration.bean;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 注册servlet
 * @author zh
 * @date 2021/9/27 17:02
 */
@Configuration
public class ServletRegistConfig {

    @Bean
    public ServletRegistrationBean registServlet(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
        // servlet
        servletRegistrationBean.setServlet(new RegistServlet());
        List<String> urlMappings = new ArrayList<>();
        urlMappings.add("/registServlet");
        // 地址映射
        servletRegistrationBean.setUrlMappings(urlMappings);
        // servlet名称
        servletRegistrationBean.setName("registServlet");

        Map<String,String> initMap = new HashMap<>();
        initMap.put("name","李四");
        // servlet的初始参数
        servletRegistrationBean.setInitParameters(initMap);

        return servletRegistrationBean;

    }
}
